package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffTalentTeamEditBean;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.model.UecTalentTeam;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecOutStaffTalentTeamEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffTalentTeamEditBean bean = new UecOutStaffTalentTeamEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecTalentTeam uecTalentTeam;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * @throws ParseException 
	 */
	public void onCreate$uecTalentTeamEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 专业人才名称glbdef1
		ListboxUtils.rendererForEdit(bean.getGlbdef1(), NodeVoUtil.getDictionaries(UecConstants.PROFESSIONAL_NAME));
		// 专业人才等级glbdef2
		ListboxUtils.rendererForEdit(bean.getGlbdef2(), NodeVoUtil.getDictionaries(UecConstants.PROFESSIONAL_LEVEL));
		// 子专业：申报专业-电信 glbdef9
		ListboxUtils.rendererForEdit(bean.getGlbdef9(), NodeVoUtil.getDictionaries(UecConstants.PROFESSIONAL_SUB_MAJOR));
		// 年度考核结果 glbdef3
		ListboxUtils.rendererForEdit(bean.getGlbdef3(), NodeVoUtil.getDictionaries(UecConstants.PROFESSIONAL_EXAM_RESULT));
		// 聘任状态 glbdef13
		ListboxUtils.rendererForEdit(bean.getGlbdef13(), NodeVoUtil.getDictionaries(UecConstants.EMP_STATUS));
		//人才类型
		List<NodeVo> nodeVoGlbdef10List = new ArrayList<NodeVo>();
		String[][] glbdef10 = {{"1", "实操"},
				{"2", "非实操"},
				{"", "无"}};
		for (int i = 0; i < glbdef10.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef10[i][j]);
				nodeVo.setName(glbdef10[i][j + 1]);
			}
			nodeVoGlbdef10List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef10(), nodeVoGlbdef10List);
		
		//是否生效
		List<NodeVo> nodeVoGlbdef8List = new ArrayList<NodeVo>();
		String[][] glbdef8 = {{"Y", "是"},
				{"N", "否"}};
		for (int i = 0; i < glbdef8.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef8[i][j]);
				nodeVo.setName(glbdef8[i][j + 1]);
			}
			nodeVoGlbdef8List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef8(), nodeVoGlbdef8List);
		//最近记录标识--lastflag
		ListboxUtils.rendererForEdit(bean.getLastflag(), nodeVoGlbdef8List);
	}

	/**
	 * bindBean
	 * @throws ParseException 
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("新增专业人才队伍信息");
			bean.getUecTalentTeamEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("查看专业人才队伍信息");
			uecTalentTeam = (UecTalentTeam) arg.get("uecTalentTeam");
			bean.getSaveBtn().setVisible(false);
			bean.getCancelBtn().setVisible(false);
			if (null != uecTalentTeam) {
				PubUtil.fillBeanFromPo(uecTalentTeam, bean);
				stringTransToDate();
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecTalentTeamEditWindow().setTitle("编辑专业人才队伍信息");
			bean.getUecTalentTeamEditDiv().setHeight("30%");
			uecTalentTeam = (UecTalentTeam) arg.get("uecTalentTeam");
			if (null != uecTalentTeam) {
				PubUtil.fillBeanFromPo(uecTalentTeam, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		//获取登录用户
		String currentName  = "";
		if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
			ZkUtil.showError("当前登录用户已经失效，请重新登录系统", "错误提示");
			return;
		}else {
			currentName = PlatformUtil.getCurrentUser().getScreenName();
		}
		
		if ("add".equals(opType)) {
			uecTalentTeam = UecTalentTeam.newInstance();
			PubUtil.fillPoFromBean(bean, uecTalentTeam);
			dataTransString();
			uecTalentTeam.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecTalentTeam.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecTalentTeam.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			
			uecTalentTeam.setCreator(currentName);
			uecTalentTeam.setCreationtime(DateUtil.getCurrenDate());
			
			uecTalentTeam.addOnly();
			Events.postEvent("onOK", this.self, uecTalentTeam);
			this.onCancel();
			Messagebox.show("新增成功");
			
			// 发起流程 uec_talent_team 新增一条记录
//			String procDefId = "outStaffTalentTeamEdit:1:bf6f02a207074f80b9b4dd4a6db3d10a";
//			Long outStaffId = uecOutStaffInfo.getOutStaffId();
//			uecTalentTeam.addStatusCdIS3();
//			Long talentTeamId = uecTalentTeam.getTalentTeamId();
//			if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
//				ZkUtil.showError("当前登录用户不存在!", "错误提示");
//				return;
//			}
//			String currentName = PlatformUtil.getCurrentUser()
//					.getScreenName();
//			String startFlowJson = "{" + "formTheme: '盟市外包人员专业人才申请流程',"
//					+ " formContent: '专业人才人员:"
//					+ uecOutStaffInfo.getPsnName() + "'," + " procDefId: '"
//					+ procDefId + "'," + " urgent: '1',"
//					+ " atiActCategoryId: '21'," + " procDefKey: '"
//					+ ApprovalUtil.OUT_STAFF_TALENT_TEAM_EDIT + "',"
//					+ " formSender: '" + currentName + "',"
//					+ " primaryId: '{" + "uec_out_staff_info_his:"
//					+ outStaffId + ",uec_out_staff_info:" + outStaffId
//					+ ",uec_talent_team:" + talentTeamId + "'}";
//
//			String procInstId = null;
//			try {
//				procInstId = ApprovalUtil.startApproval(startFlowJson);
//			} catch (Exception e) {
//				// 流程发起失败 数据改为失效
//				uecTalentTeam.removeOnly();
//				e.printStackTrace();
//			}
//
//			if (StrUtil.isNullOrEmpty(procInstId)) {
//				uecTalentTeam.removeOnly();
//				ZkUtil.showError("流程发起失败！", "错误提示");
//				return;
//			}
//			// 第一审批人如果是发起人直接到人力审批环节
//			ApprovalUtil.isCurrentAssignNames(procInstId);	
//			Events.postEvent("onOK", this.self, uecTalentTeam);
//			this.onCancel();
//			Messagebox.show("保存成功，专家人才队伍新增流程已发起！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecTalentTeam);
			dataTransString();
			
			uecTalentTeam.setModifier(currentName);
			uecTalentTeam.setModifiedtime(DateUtil.getCurrenDate());
			
			uecTalentTeam.updateOnly();
			Events.postEvent("onOK", this.self, uecTalentTeam);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecTalentTeamEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getGlbdef1().getSelectedItem().getValue())) {
		return "请选择专业人才名称";
		} else if (StrUtil.isNullOrEmpty(bean.getGlbdef2().getSelectedItem().getValue())) {
		return "请选择专业人才等级";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef3().getSelectedItem().getValue())) {
			return "请选择年度考核结果";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef4().getValue())) {
			return "请填写专家津贴金额";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef6().getValue())) {
			return "请填写股票增值权";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef10().getSelectedItem().getValue())) {
			return "请选择人才类型";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef11().getValue())) {
			return "请填写第一年考核";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef12().getValue())) {
			return "请填写第二年考核";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef13().getSelectedItem().getValue())) {
			return "请选择聘任状态";
		}else if (StrUtil.isNullOrEmpty(bean.getGlbdef9().getSelectedItem().getValue())) {
			return "请填写子专业";
		}else if (StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			return "请选择开始日期";
		}
		return null;
	}
	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void dataTransString() {
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef14().getValue())) {
			// 退出时间
			uecTalentTeam.setGlbdef14(DateUtil.dateToStr(bean.getGlbdef14().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBegindate().getValue())) {
			// 开始日期
			uecTalentTeam.setBegindate(DateUtil.dateToStr(bean.getBegindate().getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEnddate().getValue())) {
			// 结束日期
			uecTalentTeam.setEnddate(DateUtil.dateToStr(bean.getEnddate().getValue()));
		}
	}
	
	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		if (null != uecTalentTeam.getGlbdef14()) {
			bean.getGlbdef14()
					.setValue(DateUtil.patternStrToDate(uecTalentTeam.getGlbdef14()));
		}
		if (null != uecTalentTeam.getBegindate()) {
			bean.getBegindate()
					.setValue(DateUtil.patternStrToDate(uecTalentTeam.getBegindate()));
		}
		if (null != uecTalentTeam.getEnddate()) {
			bean.getEnddate()
					.setValue(DateUtil.patternStrToDate(uecTalentTeam.getEnddate()));
		}
	}

}
